package com.dzl.fee.service.impl;

import com.dzl.base.service.BaseServiceImpl;
import com.dzl.constant.CommonConstants;
import com.dzl.fee.dao.WelfareFeeUserCustomersMapper;
import com.dzl.fee.pojo.WelfareFeeOrder;
import com.dzl.fee.pojo.WelfareFeeUserCustomers;
import com.dzl.fee.pojo.WelfareFeeUserCustomersSalesRel;
import com.dzl.fee.pojo.query.WelfareFeeUserCustomersQuery;
import com.dzl.fee.pojo.vo.WelfareFeeUserCustomersVo;
import com.dzl.fee.service.WelfareFeeOrderService;
import com.dzl.fee.service.WelfareFeeUserCustomersSalesRelService;
import com.dzl.fee.service.WelfareFeeUserCustomersService;
import com.dzl.util.EmptyUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 客户信息表(WelfareFeeUserCustomers)表服务实现类
 *
 * @author wangjian
 * @date 2021-08-03 15:12:34
 */
@Service
public class WelfareFeeUserCustomersServiceImpl extends BaseServiceImpl<WelfareFeeUserCustomers> implements WelfareFeeUserCustomersService {

    @Autowired
    private WelfareFeeUserCustomersMapper mapper;
    @Autowired
    private WelfareFeeUserCustomersSalesRelService welfareFeeUserCustomersSalesRelService;
    @Autowired
    private WelfareFeeOrderService welfareFeeOrderService;

    @Override
    public PageInfo<WelfareFeeUserCustomersVo> getCustomersVoListPage(WelfareFeeUserCustomersQuery query) {
        if (EmptyUtils.isNotEmpty(query.getSalesId())){
            WelfareFeeUserCustomersSalesRel welfareFeeUserCustomersSalesRel = new WelfareFeeUserCustomersSalesRel();
            welfareFeeUserCustomersSalesRel.setSalesId(query.getSalesId());
            List<WelfareFeeUserCustomersSalesRel> select = welfareFeeUserCustomersSalesRelService.select(welfareFeeUserCustomersSalesRel);
            StringBuffer customersIds = new StringBuffer();
            if (EmptyUtils.isNotEmpty(select)) {
                select.forEach(x -> {
                    customersIds.append(x.getCustomerId()).append(",");
                });
            }
            query.setCustomersIds(customersIds.toString());
        }
        PageHelper.startPage(query);
        List<WelfareFeeUserCustomersVo> customersVoList = mapper.getCustomersVoList(query);
        if (EmptyUtils.isNotEmpty(customersVoList)){
            customersVoList.forEach(x ->{
                WelfareFeeOrder welfareFeeOrder = new WelfareFeeOrder();
                if (EmptyUtils.isNotEmpty(query.getSalesId())){
                    welfareFeeOrder.setSalesUserId(query.getSalesId());
                }
                welfareFeeOrder.setCustomersId(x.getId());
                welfareFeeOrder.setStatus(CommonConstants.YES);
                int count = welfareFeeOrderService.selectCount(welfareFeeOrder);
                x.setOrderNum(count);
            });
        }
        PageInfo<WelfareFeeUserCustomersVo> page = new PageInfo<WelfareFeeUserCustomersVo>(customersVoList);
        return page;
    }

    @Override
    public Integer updateCustomers(WelfareFeeUserCustomers welfareFeeUserCustomers) {
        if (EmptyUtils.isEmpty(welfareFeeUserCustomers.getId())){
            mapper.insertSelective(welfareFeeUserCustomers);
        }else {
            mapper.updateByPrimaryKeySelective(welfareFeeUserCustomers);
        }
        return 1;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer updateCustomersStatus(Integer id) {
        //删除客户
        WelfareFeeUserCustomers welfareFeeUserCustomers = new WelfareFeeUserCustomers();
        welfareFeeUserCustomers.setId(id);
        welfareFeeUserCustomers.setStatus(0);
        mapper.updateByPrimaryKeySelective(welfareFeeUserCustomers);
        //删除关联关系
        WelfareFeeUserCustomersSalesRel welfareFeeUserCustomersSalesRel = new WelfareFeeUserCustomersSalesRel();
        welfareFeeUserCustomersSalesRel.setCustomerId(id);
        welfareFeeUserCustomersSalesRelService.delete(welfareFeeUserCustomersSalesRel);
        return 1;
    }
}